import { createSlice } from "@reduxjs/toolkit";
import axios  from "axios";
const billStore = createSlice({
    name: 'bill',
    // 数据状态state
    initialState: {
        billList: []
    },
    reducers: {
        // 同步修改方法
        setBillList(state,action) {
            state.billList = action.payload
        },
        // 同步添加账单方法
        addBill(state,action) {
            state.billList.push(action.payload)
        }
    }
})
const {setBillList,addBill} = billStore.actions


// 编写异步
const getBillList = () => {
    return async (dispatch) => {
   const res = await  axios.get('http://localhost:9000/ka')
   dispatch(setBillList(res.data))
    }
}
const addBillList = (data) => {
    console.log(data);
    
    return async (dispatch) => {
        let res =  await  axios.post('http://localhost:9000/ka',data)
        dispatch(addBill(res.data))
    }
}
export {getBillList,addBillList}
// 结构出action
const reducer = billStore.reducer

export default reducer